//
// gcssai_stats.cpp
// gcs
// Grand Central Station, controlling process of the Protea Application Server / Business Logic Transactions for SAI Statistics
// Copyright (C) 2003, 2004, 2005 eXegeSys, Inc.
// Copyright (C) 2008 Bruce A. James
//
// This program is part of the Protea Project.
//
// The Protea Project is free software; you can redistribute it and/or modify it 
// under the terms of the GNU General Public License as published by the 
// Free Software Foundation, either version 3 of the License, or (at your 
// option) any later version.
//
// The Protea Project is distributed in the hope that it will be useful, but 
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
// for more details.
// 
// You should have received a copy of the GNU General Public License along 
// with this program.  If not, see <http://www.gnu.org/licenses/>.
// 
// Please send correspondence to:
// 
// theproteaproject@gmail.com
//

// gcssai.cpp
//
// Contains the SAI transactions, compiled into GCS
//

#include "../SDK/ToolsInterface.h"
#include "../AppHandler/parms_xml.h"
#include "gcs.h"


void SetStat( XCBuffer* buf, const char* name, const int value ) {
	buf->GetField( FLD_STAT_NAME )->SetString( name );

	char number[ 20 ];
	sprintf( number, "%d", value );
	buf->GetField( FLD_STAT_VALUE )->SetString( number );
};

void SetStat( XCBuffer* buf, const char* name, const char* value ) {
	buf->GetField( FLD_STAT_NAME )->SetString( name );
	buf->GetField( FLD_STAT_VALUE )->SetString( value );
};


//@ SEC 2
//@ CHA Review General Stats
//@ COD ReviewGeneralStats
//@ NUM 1
TRANSACTION_CODE( XCGCS::ReviewGeneralStats )
{
	XCBuffer* detail = NULL;

	START_TXN( ReviewGeneralStats );

		int child = txnvars.buffer->GetChildFormat();
                if ( child )
                {
			int count;

                        //@ Create the child buffer
                        detail = CreateBuffer( child, false );

			// Count the number of registered hosts
			count = 0;
			XCSubstation* stemp = hostlist.GetFirstHost();
			while (stemp) {
				count++;
				stemp = stemp->next;
			};
			SetStat( detail, "Registered Hosts", count );
			TransResponse( txnvars.result, detail, TRANS_DETAIL_TYPE );

			// Count the number of registered applications
			count = 0;
			XCApplicationList* atemp = hostlist.GetAppList();
			while (atemp) {
				count++;
				atemp = atemp->next;
			};
			SetStat( detail, "Registered Applications", count );
			TransResponse( txnvars.result, detail, TRANS_DETAIL_TYPE );

			// Count the number of open transactions
			count = 0;
			XCGCSTransaction* ttemp = hostlist.GetTransList()->transactions;
			while (ttemp) {
				count++;
				ttemp = ttemp->next;
			};
			SetStat( detail, "Open Transactions", count );
			TransResponse( txnvars.result, detail, TRANS_DETAIL_TYPE );

			SetStat( detail, "Load Balance Method", "Random" );
			TransResponse( txnvars.result, detail, TRANS_DETAIL_TYPE );

			SetStat( detail, "Active users", licensing.Count() );
			TransResponse( txnvars.result, detail, TRANS_DETAIL_TYPE );

			SetStat( detail, "GCS Port", listenport );

			const char* s_auth_method = "Internal";

			switch( auth_method ) {
				case AUTHENTICATE_LDAP:
					s_auth_method = "LDAP";
					break;
				case AUTHENTICATE_PAM:
					s_auth_method = "PAM";
					break;
			};
			SetStat( detail, "Authentication", s_auth_method );

			TransResponse( txnvars.result, detail, TRANS_DETAIL_TYPE );
		};
		
		txnvars.result->SetSuccess( true );

		CLEANUP_TXN_1();
                        CleanupObjects( 1, &detail );
		CLEANUP_TXN_2();
                        CleanupObjects( 1, &detail );
		CLEANUP_TXN_3();
                        CleanupObjects( 1, &detail );
	END_TXN();
};
